package com.mars.quartz.scheduler.web.controller;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mars.quartz.scheduler.jobs.BackupJob;
import com.mars.quartz.scheduler.jobs.EmailJob;
import com.mars.quartz.scheduler.utils.SchedulerUtils;

@Controller
@RequestMapping("/scheduler")
public class SchedulerController {
	
	@Autowired
	private SchedulerUtils scheduler ;
	
	@ResponseBody
	@RequestMapping(value="email")
	public void email() {
		try {
			this.scheduler.createScheduler(EmailJob.class, null);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@ResponseBody
	@RequestMapping(value="backup")
	public void backup() {
		try {
			this.scheduler.createScheduler(BackupJob.class, null);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 重置设置任务触发时间
	 */
	@ResponseBody
	@RequestMapping(value="resetCron")
	public void resetCron(String jobId, String cron) {
		try {
			this.scheduler.updateScheduleJob(jobId, cron);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 暂停任务
	 */
	@ResponseBody
	@RequestMapping(value="pause")
	public void pause(String jobId) {
		System.out.println(jobId);
		try {
			this.scheduler.pauseJob(jobId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 重新启动任务
	 */
	@ResponseBody
	@RequestMapping(value="resume")
	public void resume(String jobId) {
		System.out.println(jobId);
		try {
			this.scheduler.resumeJob(jobId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
